﻿#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
#endregion

namespace TheSpirit
{
    public class UseCard
    {
        #region Declaration

        //기본 카드의 리스트
        List<Card> cardList = new List<Card>();
        //각 카드의 숫자
        Dictionary<String, int> cardCount = new Dictionary<String, int>();
        Point position = Point.Zero;

        int count;
        //기본 카드 개수
        const int CARDNUM = 3;

        #endregion


        #region Property

        public List<Card> CardList
        {
            get { return cardList; }
        }
        public Dictionary<String, int> CardCount
        {
            get { return cardCount; }
        }

        #endregion


        #region Initialize

        public UseCard(ScreenManager screenManager)
        {
            position.X = 144;
            position.Y = 575;

            count = 0;

            AddBasicCard(screenManager);
            setPosition();
        }

        #endregion


        #region Update & Draw

        public void Update()
        {
            // 덱에서 카드 제거
            for (int i = 0; i < cardList.Count; i++)
            {
                if (Collision.IntersectPixels(
                    InputManager.MousePosition, Collision.ExtractColor(InputManager.MouseTexture()), 
                    cardList[i].Position, Collision.ExtractColor(cardList[i].CardTexture)))
                {
                    InputManager.ChangeMouseCursor("Select");

                    if (InputManager.IsLeftClicked())
                    {
                        if(cardCount[cardList[i].Name] >0)
                            cardCount[cardList[i].Name]--;
                        return;
                    }
                }
            }
        }

        public void Update(Card card, ScreenManager screenManager)
        {
            //덱의 카드 개수가 초과되었는지 체크
            if (count + 1 > Constants.UsingCardNum)
            {
                screenManager.AddScreen(new WarningScreen());
                return;
            }

            switch (card.Name)
            {
                case "FireBall":
                    cardCount["FireBall"]++;
                    break;
                case "WaterBall":
                    cardCount["WaterBall"]++;
                    break;
                case "Grass":
                    cardCount["Grass"]++;
                    break;
                case "Upgrade":
                    cardCount["Upgrade"]++;
                    break;
                default:
                    break;
            }

            count++;
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            for (int i = 0; i < cardList.Count; i++)
            {
                cardList[i].Draw(spriteBatch);
                //카드 개수 출력
                Fonts.Draw(spriteBatch, Fonts.Segoe14, 
                    cardCount[cardList[i].Name].ToString(), 
                    new Vector2(cardList[i].Position.X + cardList[i].Position.Width + 20, 
                        cardList[i].Position.Y + cardList[i].Position.Height / 2), 
                    Color.Red);
            }
        }

        #endregion


        #region Helper

        public void AddBasicCard(ScreenManager screenManager)
        {
            ContentManager content = screenManager.Game.Content;

            //Basic 카드 추가
            //Fire
            cardList.Add(new FireBallCard());
            cardCount.Add(cardList[cardList.Count - 1].Name, 0);
            cardList[cardList.Count - 1].LoadContent(content);
            //Water
            cardList.Add(new WaterBallCard());
            cardCount.Add(cardList[cardList.Count - 1].Name, 0);
            cardList[cardList.Count - 1].LoadContent(content);
            //Forest
            cardList.Add(new GrassCard());
            cardCount.Add(cardList[cardList.Count - 1].Name, 0);
            cardList[cardList.Count - 1].LoadContent(content);
            //Upgrade
            cardList.Add(new UpgradeCard());
            cardCount.Add(cardList[cardList.Count - 1].Name, 0);
            cardList[cardList.Count - 1].LoadContent(content);
        }

        public void setPosition()
        {
            int x = position.X;
            int y = position.Y;
            int w = 75;
            int h = 100;
            int l = 124;

            for (int i = 0; i < cardList.Count; i++)
            {
                cardList[i].setPosition(x, y, w, h);
                x = x + w + l;
            }
        }

        #endregion
    }
}
